package org.jeecg.modules.front.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.yinsheng.YinShengClient;
import org.jeecg.modules.yinsheng.command.base.HeadResponseCommand;
import org.jeecg.modules.yinsheng.command.base.MerchantHeadRequestCommand;
import org.jeecg.modules.yinsheng.command.jspay.JsPayReqCommand;
import org.jeecg.modules.yinsheng.command.jspay.JsPayRespCommand;
import org.jeecg.modules.yinsheng.command.wypay.WyPayRequestCommand;
import org.jeecg.modules.yinsheng.command.wypay.WyPayResponseCommand;
import org.jeecg.modules.yinsheng.common.ServiceEnum;
import org.jeecg.modules.yinsheng.common.YsPayConstant;
import org.jeecg.modules.yinsheng.utils.JsonParser;
import org.jeecg.modules.yinsheng.utils.YinshengLogger;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.UUID;

@Service
public class YinshengBank extends Payment{


    public YinshengBank(String channel) {
        // 构造方法体
        this.channel = channel;

    }

    public YinshengBank() {
        // 构造方法体

    }

    public Result<JSONObject> pay(String requestNo, Double amount, Map<String, String> extendParams)
    {

        // 先构建请求客户端
        YinShengClient yinShengClient = new YinShengClient(ServiceEnum.WALLET_PAY_FRONT_CODE, YsPayConstant.ENV_PRD, YinShengUtil.getPrdMerchantPrx(), YinShengUtil.getPrdMerchantPrxPwd(), YinShengUtil.getRsaPrd());
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(YinShengUtil.getPrdSrcMerchantNo());
        headRequestCommand.setServiceNo("netpay");
        WyPayRequestCommand wyPayRequestCommand = new WyPayRequestCommand();
        wyPayRequestCommand.setRequestNo(requestNo);//M	业务请求号
        wyPayRequestCommand.setPayeeMerchantNo(YinShengUtil.getPrdPayeeMerchantNo());//收款方银盛商户号
        wyPayRequestCommand.setOrderDesc(extendParams.get("name"));//商品名称/订单标题
        wyPayRequestCommand.setAmount(String.valueOf(amount));//交易金额，单位：元取值范围：0.01-999999.99
        wyPayRequestCommand.setBankType(extendParams.get("bankType"));//银行行别参考：支持银行列表查询接口
        wyPayRequestCommand.setPayMode(extendParams.get("payMode"));//支付方式01-PC网银支付（默认值）11-手机网银支付
        wyPayRequestCommand.setBankAccountType(extendParams.get("bankAccountType"));//银行账户类型11-个人借记卡12-个人贷记卡21-对公借记卡22-对公贷记卡
        wyPayRequestCommand.setNotifyUrl(YinShengUtil.getNotifyUrl()+"front/userHomeOrder/yinsheng/notify");//支付成功结果异步通知地址，为空则不通知支持多个url，url之间用分隔符“,”分开
        //非必填
		/*wyPayRequestCommand.setRedirectUrl("192.127.0.0.1");//重定向链接，支付成功后重定向到此链接
		wyPayRequestCommand.setNotifyUrl("192.127.0.0.1");//支付成功结果异步通知地址，为空则不通知支持多个url，url之间用分隔符“,”分开
		wyPayRequestCommand.setNotifyUrlByFail("192.127.0.0.1");//支付失败结果异步通知地址，为空则不通知支持多个url，url之间用分隔符“,”分开

		wyPayRequestCommand.setSupportCards("Y");//是否支持信用卡支付，默认值：YY-支持  N-不支持
		wyPayRequestCommand.setTranType("01");//交易类型01-即时交易（默认值）  02-担保交易
		wyPayRequestCommand.setIsDivision("N");//是否分账，未开通分账功能或交易类型为担保交易时此参数无作用Y-是（需调用分账登记接口进行分账） N-否（直接入账到收款方账户）
		wyPayRequestCommand.setTimeOut("60");//订单有效期，单位：分钟默认支付有效时间为7天
		wyPayRequestCommand.setExtendParams("111");//扩展参数，商户自定义数据域，原样返回此字段需开通权限后生效*/
        HeadResponseCommand baseResponseCommand = yinShengClient.doAction(headRequestCommand, wyPayRequestCommand);
        YinshengLogger.logBizInfo(baseResponseCommand);
        WyPayResponseCommand respCommand = JsonParser.parserJsonStringToJavaObject(baseResponseCommand.getBizResponseJson(),
                WyPayResponseCommand.class);
        String str= JsonParser.parserJavaObjectToJsonString(respCommand, false);
        JSONObject jsonObject = JSON.parseObject(str);
        if(jsonObject.get("state").equals("WAIT_PAY"))
        {
            return Result.ok(jsonObject);
        }
        else
        {
            return Result.error(String.valueOf(jsonObject.get("subMsg")));
        }

    }


    public  Integer payType()
    {
        if(this.channel.equals("wechat"))
        {
            return 6;
        }


        return 0;
    }
}
